<?php
require_once("../../resources/commons.php");
require_once("../../data/Item.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/textarea.php");
require_once("HTML/QuickForm/text.php");
require_once("HTML/QuickForm/hidden.php");
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/button.php");
require_once("HTML/QuickForm/date.php");

/**
 * Form to update an action description and title.<br/>
 * Form to be used by a user to update the comment of an action.
 */
class UpdateActionPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
		
		$l_titleInput=new HTML_QuickForm_text("title");
		$l_titleInput->setLabel("Title :");
		$l_titleInput->setSize(60);
		$this->addElement($l_titleInput);
        
		$l_descriptionInput=new HTML_QuickForm_textarea("description");
		$l_descriptionInput->setLabel("Description :");
		$l_descriptionInput->setCols(60);
		$l_descriptionInput->setRows(5);
		$this->addElement($l_descriptionInput);
		
		$l_targetDateInput=new HTML_QuickForm_date("targetDate",null,
			array(
				"format" => "Y-m-d",
				"maxYear" =>date("Y", time())+2,
				"minYear" =>date("Y", time())));
		$l_targetDateInput->setLabel("Target date:");
		$this->addElement($l_targetDateInput);
		
		$l_itemInput=new HTML_QuickForm_hidden("itemId");
		$this->addElement($l_itemInput);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		$l_cancel=new HTML_QuickForm_submit($this->getButtonName("cancel"));
		$l_cancel->setValue("Cancel");
		$this->addElement($l_cancel);
		
		$this->registerRule("checkDate", "callback", "isDateValid");
		$this->addRule("targetDate", "The specified target date is not valid", "checkDate");
		$this->addRule("title","Title must not be empty","required",null,"client");
		$this->addRule("title","Title must not be empty","required",null,"server");
		$this->addRule("description","Description must not be empty","required",null,"server");
		$this->applyFilter("description","trim");
		$this->applyFilter("title","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	displayTop("Add new comment","../../resources/htmledit.php",null);
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	$l_itemId=base64_decode($page->exportValue("itemId"));
    	$l_action=Item::getFromPrimaryKey($l_itemId);
    	
    	//Actions can be modified only by the creator of the action
    	$l_creatingUserId=$l_action->getCreatingUser_id();
		$l_me=SessionManager::getCurrentLoggedUserId();
		if($l_creatingUserId==$l_me){
	    	$l_action->setDescription($page->exportValue("description"));
	    	$l_action->setTitle($page->exportValue("title"));
	    	$l_targetDate=$page->exportValue("targetDate");
	    	$l_action->setTargetDate($l_targetDate["Y"]."/".$l_targetDate["m"]."/".$l_targetDate["d"]);
	    	$l_action->save();
		}
    	
    	HTTP::redirect("index.php");
    }
}

class CancelProcess extends HTML_QuickForm_Action{
    function perform($page){
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('UpdateActionProcess', false);
$controller->container(true);
$controller->addPage(new UpdateActionPage('updateActionPage'));
if(isset($_REQUEST["id"])){
	//Actions can be modified only by the creator of the action
	$l_action=Item::getFromPrimaryKey(base64_decode($_REQUEST["id"]));
	$l_creatingUserId=$l_action->getCreatingUser_id();
	$l_me=SessionManager::getCurrentLoggedUserId();
	if($l_creatingUserId==$l_me){
		$controller->setDefaults(array(
			"itemId" => $_REQUEST["id"],
			"description" => $l_action->getDescription(),
			"title" => $l_action->getTitle(),
			"targetDate" => $l_action->getTargetDate()));
	}else{
		displayError("Forbidden, not sufficient rights");
	}
}
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('cancel', new CancelProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>